﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using DotNetOpenAuth.Messaging;
using TheDFFL.Entities;
using TheDFFL.Services;

namespace TheDFFL.Web.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            var teams = new List<Team>();
            var accessToken = Session["WcfAccessToken"] as string;
            if (accessToken != null)
            {
                var consumer = base.CreateConsumer();
                var service = new TeamService(consumer, accessToken);
                teams = service.GetTeamsByLeagueKeys("nfl.l.146137", "nfl.l.454480").ToList();
            }

            return View(teams);
        }

        public ActionResult UserInfo()
        {
            var serviceEndpoint = new MessageReceivingEndpoint(
                "http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1",
                HttpDeliveryMethods.GetRequest
            );

            var accessToken = Session["WcfAccessToken"] as string;
            if (accessToken != null)
            {
                var consumer = base.CreateConsumer();
                var request = consumer.PrepareAuthorizedRequest(serviceEndpoint, accessToken);
                var response = consumer.Channel.WebRequestHandler.GetResponse(request);
                var result = XDocument.Load(response.GetResponseReader());
                ViewBag.Result = result;
            }

            return View();
        }
    }
}
